package tool;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import bean.EntityDO;

public class RequestFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        String read = request.getParameter("read");
        String write = request.getParameter("write");
        if (Objects.nonNull(read) && read.length() > 0) {
            if (read.getBytes("UTF-8").length <= EntityDO.MAX_ENTITY_NAME_LENGTH) {
                chain.doFilter(request, response);
                return;
            }
        }
        if (Objects.nonNull(write) && write.length() > 0) {
            if (write.getBytes("UTF-8").length <= EntityDO.MAX_ENTITY_NAME_LENGTH) {
                chain.doFilter(request, response);
                return;
            }
        }
        PrintWriter out = response.getWriter();
        out.print("-1");
        out.close();
    }

}
